package y2016.date0510.pm.数组闭环替换;

import org.junit.jupiter.api.Test;

public class ResplaceNums {
    private static final int ARRAY[][] = {
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
            {0, 1, 0, 1, 1, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0},
            {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
            {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0},
            {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0},
            {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0},
            {0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    };
    private static int biNum;


    @Test
    public void main() throws Exception {
        StringBuffer nc = new StringBuffer();
        int[][] nums = ARRAY.clone();//比较复杂的逻辑，防止数据出错不能挽回
        for (int i = 0; i < nums.length; i++) {
            int[] hang = nums[i];
            for (int j = 0; j < hang.length; j++) {
                int ge = hang[j];
                nc.append(ge + " ");
            }
            nc.append("\n");
        }
        System.out.println(nc);//输出初始的矩阵


        for (int i = 0; i < nums.length; i++) {//一行行遍历二维数组

            int[] hang = nums[i];//通过遍历可以取出每一行数组
            int flagA = 0;
            int flagB = 0;
            int timesA = 0;
//			int timesB = 0;

            for (int j = 0; j < hang.length; j++) {//对每一行数组遍历

                int ge = hang[j];//通过遍历可以取出每一个数

                if (j + 1 < hang.length) {


                    if (ge == 1 && hang[j + 1] == 0) {//遇到形如：……10这种情况

                        if (timesA == 0) {//如果是第一次遇到，则记录下标
                            flagA = j + 1;
                        }
                        timesA++;//次数增加....
                    } else if (ge == 0 && hang[j + 1] == 1) {//遇到形如：……01这种情况

                        if (timesA > 0) {//当出现过形如……10的情况后，……10这种情况形成行闭合
                            flagB = j + 1;

                            /*
                             * 出现行闭合的情况，则可能需要更改中间段的值
                             * 需要做判断，当符合行闭合的情况时，判断上层与下层是否存在闭合，如果存在则开始替换值。不存在则跳过
                             */
                            if (flagB > flagA) {

                                for (int k = flagA; k < flagB; k++) {//遍历下标为k的所有上下层
                                    boolean topFlag = false;
                                    boolean buttomFlag = false;
                                    for (int l = 0; l < nums.length; l++) {//获取每一层

                                        int[] ks = nums[l];//每一行
                                        if (ks[k] == 1 && i > l && topFlag == false) {//i为第一个for循环参数，代表当前遍历行数，i>l表示在当前行上方
                                            topFlag = true;//上方只要存在一行在k位置的值为1，改变topFlag标记的值
                                        }
                                        if (ks[k] == 1 && i < l && buttomFlag == false) {//
                                            buttomFlag = true;//下方只要存在一行在k位置的值为1，改变buttomFlag标记的值
                                        }
                                    }


                                    if (topFlag && buttomFlag && hang[k] == 0) {//只有等于0时才替换
                                        biNum++;
                                        hang[k] = 2;

                                    }
//								if(topFlag == false || buttomFlag == false){
//									System.out.println("不存在闭环");
//									return;
//								}
                                }
                            }

                        }
                    }


                }
            }
        }
        /*
         * 遍历完毕，输出数组
         */
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < nums.length; i++) {
            int[] hang = nums[i];
            for (int j = 0; j < hang.length; j++) {
                int ge = hang[j];
                if (ge == 2) {//改动输出，将2变成，以便更醒目

                    sb.append("② ");
                } else {
                    sb.append(ge + "　");
                }
            }
            sb.append("\n");
        }
        System.out.println(sb);
    }
}


